<script src="{{ getAssets }}js/bivariatetable/bivariatetable.js" type="text/javascript"></script>
<script type="text/javascript">
var goods_id = '{{.goods_id}}';
var entry_id = '{{.entry_id}}';
var middle_id = '{{.middle_id}}';
var goods_function_info;
var goods_mouting_genre;
var typeid = 30;
$(function(){
	$.post('{{ urlfor "ErpEntryController.MountingAdd" }}',{'goods_id':goods_id, 'middle_id': middle_id}, function(res){
		if(res.code == 0) {
			$('#goods-title').html(productGroupName(res.data.goods));
			middle_data = res.data.middle;
			typeid = res.data.goods.TypeId;
			$.post('{{ urlfor "ProductsController.GetOptions"}}', {'fields':['goods_function_info', 'goods_mouting_genre']}, function(res){
				goods_mouting_genre = res.data.goods_mouting_genre;
				goods_function_info = res.data.goods_function_info;
				if(middle_data == null) {
					for(i=0;i<=7;i++){
						$('#addline').click();
					}
				} else {
					$('#attrlist').html('');
					$('#addsndselect').hide();
					$.each(middle_data, function(index, value){
						addTrLine(value);
					});	
				}
				colShowHide();
			});	
		}
	}, 'json');
	
});

function colShowHide() {
	if(typeid == 30) {
		$('.f40').hide();
	} else if(typeid == 41){
		$('.f40').show();
	}	
}
function setOptions(data ,def) {
	var opt = '<option value=""></option>';
	$.each(data, function(index, value){
		opt += '<option value="'+ value.val +'" '+ (def==value.val ? 'selected':'') +'>'+ value.val +'</option>';
	});
	return opt;
}

function addTrLine(data) {
    var tr = '<tr><td><input type="checkbox" value=""  class="checkboxs"/><input type="hidden" value="'+ (data.Snd ? data.Snd.id : 0) +'" class="snd_id"/></td>\
			 <td><input name="model_number[]" class="model_number" type="text"  value="'+ (data.Snd ? data.Snd.model :'') +'" /></td>\
			 <td><input name="colour_number[]" class="colour_number" type="text"  value="'+ (data.Snd ? data.Snd.color :'') +'" /></td>\
			 <td><input name="size[]" class="size" type="text"  value="'+ (data.Snd ? data.Snd.size :'') +'" /></td>\
			 <td width="130" class="f40"><select name="function_info[]" class="function_info">'+ setOptions(goods_function_info, (data.Snd ? data.Snd.function_info :'')) +'</select></td>\
			 <td class="f40"><input name="film_color[]" class="film_color" type="text"  value="'+ (data.Snd ? data.Snd.film_color :'') +'" /></td>\
			 <td width="80"><select class="genre" name="genre[]">'+ setOptions(goods_mouting_genre, (data.Snd ? data.Snd.genre :'')) +'</select></td>\
			 <td><input name="purchase_num[]" class="purchase_num" type="number"  value="'+ data.purchase_num +'" /></td></tr>';
	$('#attrlist').append(tr);		 
	return tr;
}

function setSelectSndLine(data) {
    $('#attrlist').html('');
    $.each(data, function(index, value){
        var isset = $('.snd_id').find('input[value="'+ value.id +'"]');
        if(isset.length <= 0) {
            var _data = {};
            _data.Snd = value;
            _data.purchase_num = 1;
            addTrLine(_data);
        }
    });
    colShowHide();
}

$('#addline').click(function(){
	addTrLine({});
	colShowHide();
	return false;	
});

$('#delline').click(function(){
	var checkboxs = $('.checkboxs:checked');
	if(checkboxs.length > 0) {
		checkboxs.each(function(index, element) {
           $(this).parent().parent().remove(); 
        });
	}
	return false;	
});

$('#addsndselect').click(function () {
    var url = '{{urlfor "ProductsController.SndList"}}?product_id='+goods_id;
    layer.open({
        type: 2,
        shade: 0.2,
        title:false,
        area: ['80%', '90%'],
        maxmin: false,
        content: url,
        zIndex: parent.layer.zIndex,
        success: function(layero){
            parent.layer.setTop(layero);
        }
    });
});

$('#submitBtn').click(function(){
	var tr = $('#attrlist > tr');
	var yesLine = 0;
	var yesSubmit = true;
	var totoalnumber = 0;
	var dataInfo = new Array();
	tr.each(function(index,value){
		var valueObj = $(value);
		var model_number = valueObj.find('.model_number').val();
		var colour_number = valueObj.find('.colour_number').val();
		var genre = valueObj.find('.genre').val();
		var purchase_num = valueObj.find('.purchase_num').val();
		var function_info = valueObj.find('.function_info').val();
		var film_color = valueObj.find('.film_color').val();
		var size = valueObj.find('.size').val();
		var snd_id = valueObj.find('.snd_id').val();
		if(model_number != '') {
			if(colour_number == '') {
				layer.alert('请填写色号',function(i){
					$('#attrlist > tr').eq(index).find('.colour_number').focus();
					layer.close(i);
				});
				yesSubmit = false;
				return;
			}
			if(genre == '') {
				layer.alert('请选择框形',function(i){
					$('#attrlist > tr').eq(index).find('.genre').focus();
					layer.close(i);
				});
				yesSubmit = false;
				return;
			}
			if(purchase_num == '') {
				layer.alert('请填写采购数量',function(i){
					$('#attrlist > tr').eq(index).find('.purchase_num').focus();
					layer.close(i);
				});
				yesSubmit = false;
				return;
			}
			totoalnumber += Number(purchase_num);
			dataInfo.push({'SndId':Number(snd_id),'ModelNumber':model_number,'ColourNumber':colour_number,'Size':size, 'FunctionInfo':function_info,'FilmColor':film_color,'Genre':genre,'purchase_num':Number(purchase_num)});
			yesLine++;
		}
	});
	if(!yesSubmit) {
	    return false;
    }
	if(yesSubmit && yesLine==0){
		layer.alert('请填入数据再保存');
		return;
	}
	$.post('{{ urlfor "ErpEntryController.SaveEntryData" }}', {'purchase_num': totoalnumber, 'dataInfo':JSON.stringify(dataInfo), 'middle_id': middle_id,'goods_id':goods_id,'entry_id':entry_id}, function(res){
		if(res.code == 1) {
			layer.alert(res.msg);
			return false;	
		}
		parent.closeSelectComplete();
	}, 'json');
	
	return false;
});
$('#pasteline').click(function () {
    layer.msg("试一试 Ctrl+V 有惊喜！");
});
document.addEventListener("paste", function () {
    if (event.clipboardData || event.originalEvent) {
        var clipboardData = (event.clipboardData || window.clipboardData);
        var val = clipboardData.getData('text');
        if(val == '') {
            layer.msg("没有获取到内容");return false;
        }
        var rows = val.split("\r\n");
        $('#attrlist').html('');
        $.each(rows, function(idx, value) {
            var column = value.split("\t");
            var snd = {};
            var columns = [];
            $.each(column, function (k, v) {
                if(v == '' || v == undefined || v == 'undefined') {
                }else {
                    columns.push(v);
                }
            });
            console.log(columns);
            if(columns.length > 0) {
                snd['Snd'] = {'id':0, 'model':columns[0], 'color':columns[1], 'size':'', 'function_info':'','film_color':'','genre':'全框'};
                snd['purchase_num'] = columns[2] ? columns[2] : 0;
                addTrLine(snd);
            }
        });
        event.preventDefault();
    }
});
</script>